<?php
/**
 * Created by PhpStorm.
 * User: czw
 * Date: 2018/7/11
 * Time: 14:59
 */
namespace app\common\model;


class Exchange extends BaseModel
{
    public function product()
    {
        return $this->belongsTo('product')->bind([
            'product_name'=>'name',
            'price',
            'img_url'
        ]);
    }
    public function getConsumeTimeAttr($value)
    {
        return $value?date('Y-m-d H:i',$value):'';
    }
    public function getCreateTimeAttr($value)
    {
        return date('Y-m-d H:i',$value);
    }
    public function getStartTimeAttr($value)
    {
        return $value?date('Y-m-d',$value):'';
    }
    public function getEndTimeAttr($value)
    {
        return $value?date('Y-m-d',$value):'';
    }
    public function store(){
        return $this->belongsTo('store')->bind([
            'store_name'=>'name',
        ]);
    }
    public static function getByUserId($user_id,$where=[]){
        return self::where(['user_id'=>$user_id])->where($where)->with('product')->order('create_time DESC')->select();
    }
    public function getProductsByUserId($user_id)
    {
        return self::where(['user_id' => $user_id])->select();
    }
    public static function getAll($where){
        $model = new self();
        //名字采用模糊查询
        if (isset($where['nickname'])) {
            $where['nickname'] = ['like', '%' . $where['nickname'] . '%'];
        }
        //页码信息
        if (isset($where['page']) && isset($where['size'])) {
            $page = $where['page'] . ',' . $where['size'];
        }
        unset($where['page']);
        unset($where['size']);
        $total = $model->where($where)->count();
        if (isset($page)) {
            $model->page($page);
        }
        $res = $model->where($where)->with('product')->order('create_time DESC')->select();
        return ['data' => $res, 'total' => $total];

    }
}